package obecto.utility
{
	import flash.system.ApplicationDomain;
	import flash.utils.getQualifiedClassName;
	
	public class CloneUtility
	{
		public static function clone(instance : Object) : Object
		{
			var className : String = getQualifiedClassName(instance);
			var type : Class = ApplicationDomain.currentDomain.getDefinition(className) as Class;
			var result : Object = new type();
			
			var accessors : Array = ReflectionUtility.getNonstaticReadWriteAccessors(instance);
			for each (var accessor : String in accessors)
			{
				result[accessor] = instance[accessor];
			}
			
			var properties : Array = ReflectionUtility.getNonstaticProperties(instance);
			for each (var property : String in properties)
			{
				result[property] = instance[property];
			}
			
			return result;
		}

	}
}